package com.ss.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.ss.common.Console;
import com.ss.common.MapResult;
import com.ss.common.util.JsonConvert;
import com.ss.common.util.JsonUtil;
import com.ss.common.util.MyToolUtil;
import com.ss.user.pojo.UserInfo;
import com.ss.user.service.RoleService;
import com.ss.user.service.UserService;
/**
 * 用户管理Controller
 * @author Shanshan
 * @date 2014.03.30
 */
@Controller
@RequestMapping("user/")
public class UserController {
	
	@Autowired
	UserService service;//用户
	@Autowired
	private RoleService roleService;//角色管理
	/**
	 * 登录
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login.do")
	public ModelAndView login(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		String name=MyToolUtil.convertNull(request.getParameter("userName"));
		String password=MyToolUtil.convertNull(request.getParameter("password"));
		UserInfo info = new UserInfo();
		info.setUser_name(name);
		info.setUser_password(password);
		MapResult mr= service.login(info);
		if(mr.isIfSuccess()){
			info=((UserInfo)mr.getObj());
			request.getSession().setAttribute(Console.SESSION_KEY_USER_INFO, info);
			return new ModelAndView(new RedirectView("../index.jsp#1/m00"));
		}else{
			Map<String, String> mapResult=new HashMap<String, String>();
			mapResult.put("msg", mr.getMsg());
			return new ModelAndView("login",mapResult);
		}
	}
	
	/**
	 * 登录
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryUser.do")
	public ModelAndView queryUser(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String name=MyToolUtil.convertNull(request.getParameter("username")).trim();
		UserInfo info = new UserInfo();
		info.setUser_name(name);
		List<UserInfo> list=service.queryUser(info);
		mapResult.put("list",list);
		return new ModelAndView("/user/userManage",mapResult);
	}
	
	/**
	 * 根据用户id查询用户信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryUserById.do")
	public ModelAndView queryUserById(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String id=MyToolUtil.convertNull(request.getParameter("id"));
		String msg="";
		if(!"".equals(id)){
			UserInfo info= service.getUserById(id);
			mapResult.put("user",info);
		}else{
			msg="非法参数";
		}
		mapResult.put("msg", msg);
		return new ModelAndView("/user/userEdit",mapResult);
	}
	
	/**
	 * 保存用户
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveUser.do")
	public void saveUser(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String action=MyToolUtil.convertNull(request.getParameter("action"));
		String user_name=MyToolUtil.convertNull(request.getParameter("user_name"));
		String real_name=MyToolUtil.convertNull(request.getParameter("real_name"));
		String user_password=MyToolUtil.convertNull(request.getParameter("password"));
		String is_super=MyToolUtil.convertNull(request.getParameter("is_super"));
		UserInfo info = new UserInfo();
		info.setUser_name(user_name);
		info.setReal_name(real_name);
		info.setUser_password(user_password);
		info.setIs_super(is_super);
		//新增
		String msg="";
		if("add".equals(action)){
			service.addUser(info);
			msg="保存成功";
		}else{
			String id=MyToolUtil.convertNull(request.getParameter("id"));
			info.setId(id);
			boolean ret=service.updateUser(info);
			msg=ret==true?"保存成功":"修改失败";
		}
		mapResult.put("msg",msg);
		JsonUtil.outputJson(mapResult, response);
	}
	
	
	/**
	 * 审核
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/auditUser.do")
	public void auditUser(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String id=MyToolUtil.convertNull(request.getParameter("id"));
		String status=MyToolUtil.convertNull(request.getParameter("status"));
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id", id);
		map.put("status", status);
		String msg="审核失败";
		boolean b=service.auditUser(map);
		if(b){
			msg="审核成功";
		}
		mapResult.put("msg", msg);
		mapResult.put("id",id);
		mapResult.put("status",status);
		JsonUtil.outputJson(mapResult, response);
	}
}
